const fs = require('fs');
const path = require('path');

// 读取JSON文件
function readJSONFile(filePath) {
    try {
        const data = fs.readFileSync(filePath, 'utf8');
        return JSON.parse(data);
    } catch (error) {
        console.error(`读取文件 ${filePath} 失败:`, error);
        throw error;
    }
}

// 为树形结构的每个层级添加几何数据
function addGeometryToTree() {
    try {
        // 读取树形结构数据
        const treeData = readJSONFile('./nanjing_tree_structure.json');
        
        // 读取聚合后的城市、区县、乡镇数据
        const cityAggregatedData = readJSONFile('./nanjing_city_aggregated.json');
        const countyAggregatedData = readJSONFile('./nanjing_county_aggregated.json');
        const townAggregatedData = readJSONFile('./nanjing_town_aggregated.json');
        
        // 创建几何数据映射
        const cityGeometry = cityAggregatedData.features[0]?.geometry || null;
        
        const countyGeometryMap = new Map();
        countyAggregatedData.features.forEach(feature => {
            const properties = feature.properties || {};
            // 使用countyCode作为键
            const countyCode = properties.countyCode || feature.id || '';
            if (countyCode) {
                countyGeometryMap.set(countyCode, feature.geometry);
            }
        });
        
        const townGeometryMap = new Map();
        townAggregatedData.features.forEach(feature => {
            const properties = feature.properties || {};
            // 使用townCode作为键
            const townCode = properties.townCode || feature.id || '';
            if (townCode) {
                townGeometryMap.set(townCode, feature.geometry);
            }
        });
        
        // 更新城市层级的几何数据
        treeData.geometry = cityGeometry;
        
        // 更新区县层级的几何数据
        treeData.children.forEach(countyNode => {
            const countyCode = countyNode.id;
            if (countyGeometryMap.has(countyCode)) {
                countyNode.geometry = countyGeometryMap.get(countyCode);
            }
            
            // 更新乡镇层级的几何数据
            countyNode.children.forEach(townNode => {
                const townCode = townNode.id;
                if (townGeometryMap.has(townCode)) {
                    townNode.geometry = townGeometryMap.get(townCode);
                }
            });
        });
        
        // 保存更新后的树形结构数据
        fs.writeFileSync(
            './nanjing_tree_structure_with_geometry.json',
            JSON.stringify(treeData, null, 2),
            'utf8'
        );
        
        console.log('树形结构几何数据添加成功！');
        console.log(`已为 ${treeData.children.length} 个区县添加几何数据`);
        
        // 统计添加的乡镇几何数据数量
        const townCount = treeData.children.reduce((sum, county) => {
            const townsWithGeometry = county.children.filter(town => town.geometry !== null).length;
            return sum + townsWithGeometry;
        }, 0);
        
        console.log(`已为 ${townCount} 个乡镇添加几何数据`);
        
    } catch (error) {
        console.error('添加几何数据失败:', error);
    }
}

// 执行添加几何数据
addGeometryToTree();